package cn.sylinx.horm.example.section11;

import java.util.Arrays;
import java.util.Map;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import cn.sylinx.horm.resource.ClasspathSqlResource;
import cn.sylinx.horm.resource.parse.DefaultSqlParser;
import cn.sylinx.horm.resource.parse.SqlParser;
import cn.sylinx.horm.util.Pair;
import cn.sylinx.horm.util.ParamMapBuilder;

@Profile("section11")
@Component
public class Section11 implements CommandLineRunner {

    public Section11() {
    }

    @Override
    public void run(String... args) throws Exception {
        testForeachString();
    }
    
    void testForeachString() {

        String sql = "horm/func/foreach_string.sql";
        SqlParser sqlParser = new DefaultSqlParser();
        Map<String, Object> params = new ParamMapBuilder().put("numberList", Arrays.asList("a", "b", "c")).build();
        Pair p = sqlParser.parseSql(ClasspathSqlResource.apply(sql), params);

        System.out.println(p.getFirst().toString());
        System.out.println("-------------");

    }

    void testForeachNumber() {

        String sql = "horm/func/foreach_number.sql";
        SqlParser sqlParser = new DefaultSqlParser();
        Map<String, Object> params = new ParamMapBuilder().put("idList", Arrays.asList(1, 2, 3)).build();
        Pair p = sqlParser.parseSql(ClasspathSqlResource.apply(sql), params);

        System.out.println(p.getFirst().toString());
        System.out.println("-------------");

    }
    
    void testForeach() {

        String sql = "horm/func/foreach.sql";
        SqlParser sqlParser = new DefaultSqlParser();
        Map<String, Object> params = new ParamMapBuilder().put("numberList", Arrays.asList("a", "b", "c")).build();
        Pair p = sqlParser.parseSql(ClasspathSqlResource.apply(sql), params);

        System.out.println(p.getFirst().toString());
        System.out.println("-------------");

    }

    void testIncResource() {

        String sql3 = "horm/func/t3.sql";
        SqlParser sqlParser = new DefaultSqlParser();
        Pair p = sqlParser.parseSql(ClasspathSqlResource.applyAviatorResource(sql3), null);

        System.out.println(p.getFirst().toString());
        System.out.println("-------------");

    }

    void testNotNull() {

        String sql2 = "horm/func/t2.sql";
        SqlParser sqlParser = new DefaultSqlParser();
        Pair p = sqlParser.parseSql(ClasspathSqlResource.applySystemResource(sql2), null);

        System.out.println("when a is null:");
        System.out.println(p.getFirst().toString());
        System.out.println("-------------");

        p = sqlParser.parseSql(ClasspathSqlResource.applySystemResource(sql2),
                new ParamMapBuilder().put("a", "1").build());
        System.out.println("when a is not null:");
        System.out.println(p.getFirst().toString());

    }

    void testIsNull() {

        String sql1 = "horm/func/t1.sql";
        SqlParser sqlParser = new DefaultSqlParser();
        Pair p = sqlParser.parseSql(ClasspathSqlResource.applySystemResource(sql1), null);

        System.out.println("when a is null:");
        System.out.println(p.getFirst().toString());
        System.out.println("-------------");

        p = sqlParser.parseSql(ClasspathSqlResource.applySystemResource(sql1),
                new ParamMapBuilder().put("a", "1").build());
        System.out.println("when a is not null:");
        System.out.println(p.getFirst().toString());

    }
}
